<template>
	<view class="reminder-bg">
		<!-- <view class="nav-bar">
			<uni-icons class="back-icon" type="left" size="36rpx" color="#00CC71" @click="onBack" />
			<text class="nav-title">社区药房</text>
		</view> -->
		<view class="reminder-search-box">
			<uni-icons class="search-icon" type="search" size="40rpx" color="#00CC71"></uni-icons>
			<input class="reminder-search-input" placeholder="输入药名/条码/公司搜索药品" v-model="searchKey" />
		</view>
		<view class="reminder-section-label">
			<text class="reminder-dot"></text>
			<text class="reminder-section-title">我的提醒</text>
		</view>
		<view class="reminder-list">
			<view v-for="item in reminders" :key="item.rid" class="reminder-card">
				<view class="reminder-row row-between">
					<text class="reminder-drug">{{ item.medname }}</text>
					<button v-if="!item.icsurl || item.icsurl === 'null'" class="reminder-btn"
						@click="generateIcs(item)">生成日程提醒ics</button>
					<view v-else class="reminder-url-row">
						<text class="reminder-url">{{ item.icsurl }}</text>
						<button class="reminder-copy-btn" @click="copyUrl(item.icsurl)">
							<text class="iconfont">&#xe65c;</text>
						</button>
					</view>
				</view>
				<view style="height: 12rpx;"></view>
				<button class="reminder-delete-btn" @click="deleteReminder(item.rid)">删除</button>
			</view>
		</view>
		<view class="reminder-add-btn" @click="goAdd">

			<uni-icons class="back-icon" type="plus" size="36rpx" color="#00CC71" /> +添加用药提醒
		</view>
	</view>
</template>
<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import {
		onShow
	} from '@dcloudio/uni-app'
	const searchKey = ref('')
	const reminders = ref({})

	function deleteReminder(rid) {
		uni.showModal({
			title: '提示',
			content: '确定要删除该提醒吗？',
			success: (res) => {
				if (res.confirm) {
					uni.request({
						url: 'http://localhost:3010/reminder/del',
						method: 'POST',
						header: {
							'content-type': 'application/x-www-form-urlencoded',
							'Authorization': 'Bearer ' + uni.getStorageSync('token')
						},
						data: {
							rid
						},
						success: (res2) => {
							if (res2.data && res2.data.code === 200) {
								uni.showToast({
									title: '删除成功',
									icon: 'success'
								})
								fetchReminders()
							} else {
								uni.showToast({
									title: '删除失败',
									icon: 'none'
								})
							}
						},
						fail: () => {
							uni.showToast({
								title: '网络错误',
								icon: 'none'
							})
						}
					})
				}
			}
		})
	}

	function goAdd() {
		uni.navigateTo({
			url: '/pages/AddYongyao/AddYongyao'
		})
	}

	const onBack = () => {
		uni.navigateBack()
	}

	function copyUrl(url) {
		uni.setClipboardData({
			data: url
		})
	}

	function generateIcs(item) {
		uni.showToast({
			title: '生成ics',
			icon: 'none'
		})
	}

	function fetchReminders() {
		uni.request({
			url: 'http://localhost:3010/reminder/list',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'Authorization': 'Bearer ' + uni.getStorageSync('token')
			},
			success: (res) => {
				if (res.data && typeof res.data === 'object') {
					reminders.value = Object.values(res.data.data)
				} else {
					reminders.value = []
				}
			},
			fail: () => {
				reminders.value = []
			}
		})
	}

	function formatDate(val) {
		if (!val) return ''
		// 只取日期部分
		return val.split('T')[0]
	}
	onMounted(() => {
		fetchReminders()
	})
	onShow(() => {
		fetchReminders()
	})
</script>
<style scoped lang="scss">
	.reminder-bg {
		min-height: 100vh;
		background: #fff;
		padding: 0 0 32rpx 0;
	}

	.nav-bar {
		display: flex;
		align-items: center;
		height: 44px;
		padding: 0 16px;
		background-color: #fff;
		border-bottom: 1px solid #eee;
		margin-top: 90rpx;
	}

	.nav-title {
		margin-left: 8px;
		font-size: 18px;
		font-weight: bold;
		color: #00CC71;
	}

	.reminder-header {
		height: 120rpx;
		display: flex;
		align-items: center;
		padding-left: 40rpx;
		background: #fff;
		box-shadow: 0 8rpx 24rpx #e0f7ef;
	}

	.reminder-title {
		font-size: 44rpx;
		font-weight: bold;
		color: #00CC71;
	}

	.reminder-search-box {
		display: flex;
		align-items: center;
		background: #fff;
		border-radius: 32rpx;
		box-shadow: 0 8rpx 24rpx #00CC71;
		margin: 32rpx 32rpx 0 32rpx;
		padding: 0 24rpx;
		height: 80rpx;
	}

	.search-icon {
		font-size: 40rpx;
		color: #00CC71;
		margin-right: 16rpx;
	}

	.reminder-search-input {
		flex: 1;
		border: none;
		font-size: 30rpx;
		color: #888;
		background: transparent;
	}

	.reminder-section-label {
		display: flex;
		align-items: center;
		margin: 32rpx 0 0 32rpx;
	}

	.reminder-dot {
		width: 12rpx;
		height: 12rpx;
		background: #00CC71;
		border-radius: 50%;
		margin-right: 12rpx;
	}

	.reminder-section-title {
		font-size: 28rpx;
		color: #888;
		font-weight: bold;
	}

	.reminder-list {
		margin: 24rpx 32rpx 0 32rpx;
	}

	.reminder-card {
		background: #fff;
		border: 2rpx solid #00CC71;
		border-radius: 24rpx;
		margin-bottom: 32rpx;
		padding: 32rpx 28rpx 32rpx 28rpx;
		box-shadow: 0 4rpx 16rpx #e0f7ef;
		display: flex;
		flex-direction: column;
		justify-content: center;
		position: relative;
		/* 新增，确保删除按钮绝对定位 */
	}

	.reminder-row {
		display: flex;
		align-items: center;
		margin-bottom: 0;
	}

	.row-between {
		justify-content: space-between;
	}

	.reminder-drug {
		font-size: 36rpx;
		font-weight: bold;
		color: #222;
		flex: 1;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.reminder-btn {
		background: #00CC71;
		color: #fff;
		border-radius: 32rpx;
		font-size: 28rpx;
		font-weight: bold;
		padding: 0 36rpx;
		height: 64rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border: none;
		box-shadow: 0 2rpx 8rpx #b2ffe0;
	}

	.reminder-url-row {
		display: flex;
		align-items: center;
	}

	.reminder-url {
		background: #00CC71;
		color: #fff;
		border-radius: 16rpx;
		font-size: 26rpx;
		padding: 0 18rpx;
		margin-right: 12rpx;
		display: inline-block;
		height: 48rpx;
		line-height: 48rpx;
	}

	.reminder-copy-btn {
		background: #00CC71;
		color: #fff;
		border-radius: 16rpx;
		font-size: 28rpx;
		padding: 0 18rpx;
		margin-left: 8rpx;
		height: 48rpx;
		display: flex;
		align-items: center;
		border: none;
	}

	.reminder-delete-row {
		position: absolute;
		top: 0;
		right: 0;
		height: 100%;
		display: flex;
		align-items: center;
		z-index: 10;
		background: transparent;
		padding-right: 0;
	}

	.reminder-delete-btn {
		margin-top: 18rpx;
		width: 120rpx;
		height: 56rpx;
		background: #ff5a5f;
		color: #fff;
		border: none;
		border-radius: 28rpx;
		font-size: 28rpx;
		font-weight: bold;
		box-shadow: 0 2rpx 8rpx #ffd6d6;
		align-self: flex-end;
		display: block;
		line-height: 51rpx;
		margin-left: 500rpx;
	}

	.reminder-add-btn {
		margin: 48rpx auto 0 auto;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 36rpx;
		color: #00CC71;
		font-weight: bold;
		background: none;
		border: none;
	}

	.add-icon {
		font-size: 44rpx;
		margin-right: 12rpx;
	}
</style>